#
# relnote2html.pl
#
# Copyright 1996-2002, Silicon Graphics, Inc.
# All Rights Reserved.
#
# This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
# the contents of this file may not be disclosed to third parties, copied or
# duplicated in any form, in whole or in part, without the prior written
# permission of Silicon Graphics, Inc.
#
# RESTRICTED RIGHTS LEGEND:
# Use, duplication or disclosure by the Government is subject to restrictions
# as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
# and Computer Software clause at DFARS 252.227-7013, and/or in similar or
# successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
# rights reserved under the Copyright Laws of the United States.
#
# requires perl5 and format2html.pl
#

require 'format2html.pl';


#######################################################################
#
# void relnotes2html($root, $fname, $srch_str)
#
#######################################################################

sub relnotes2html {

    # Supports input in the following forms:
    #
    # /usr/relnotes/dmedia/ch1.z
    # TC desktop_eoe
    # desktop_eoe

    my($in_rnroot, $in_fname, $in_srch, $in_bld) = @_;

    if( $in_fname eq '' ) {
        print "relnotes2html: filename not specified" if($_WEB == 0);
        return;
    }

    my($rnroot,$product,$productbase,$tmp,$toc,$rncmd) = '';
    if($in_rnroot ne '/' && $in_rnroot ne '') {
       $rnroot = $in_rnroot; 
    }

    my($rnpath) = "$rnroot/usr/relnotes";
    $ENV{'RELNOTESPATH'} = $rnpath;

    &buildExpression($in_srch);

    my($fname) = $in_fname;
    $fname =~ s/^TC[\s]+//;


    # support for HTML release notes (a gzipped package)
    #
    if ($fname =~ /\.gz$/m){

        if($rnroot && $fname !~ /^$rnroot/){
	   &outputHTMLPage("$rnroot/$fname");
        }else{
	   &outputHTMLPage($fname);
        }
	return; 
    }

    if($fname =~ /\/([^\/]+)\/ch\w+\.z/) {

       $product = $1;
       $tmp = $fname;

       if (-e $tmp && $product =~ /^$rnpath/){
           $tmp =~ s/ch(0)?1.z//;
           $productbase = $tmp;
           $toc = $fname;
           $toc =~ s/ch(0)?1.z/TC/;
       } else {
           $productbase = "$rnpath/$product";
           $toc = "$rnpath/$product/TC";
       }

    } else {  

        $product = $fname;
        if( $product =~ /\// ) {
            $fname =~ /\/([^\/]+)$/;
            $product = $1;
        }
        $productbase = $rnpath . '/' . $product;
        my($htmlfile) = $productbase . '/' . $product . '.gz';

        if( -r $htmlfile){
            &outputHTMLPage($htmlfile);
            return;
        } else {

            $toc = $productbase . '/TC';
            if (-e $toc){

                ;  # continue on

            } else {

                # need to find this, in a *case-insensitive* way
                #
                $rncmd = "/sbin/ls -1 $rnpath | grep -i \"$product\" |";
                my(@rnfiles) = &findRnMatches($productbase, $rncmd, $rnpath);

                # release note found
                #
                if ($#rnfiles == 0) {

                    $htmlfile = $rnfiles[0] . '/' . $product . '.gz';
                    if( -r $htmlfile){
                        &outputHTMLPage($htmlfile);
                        return;
                    } else {

                        $productbase = $rnfiles[0];
                        $toc = $rnfiles[0] . '/TC';
                    }

                } elsif ($#rnfiles == -1) {

                    # No release notes found
                    #
                    &HTMLHeaderFormat("TPL");
                    print "Unable to find release notes for: $product";
                    &HTMLTrailerFormat();
                    return;
                }
            }
        }
    }

    my(@chapter) = &buildRnChapterList($productbase, $toc);  # Read TOC
    if($#chapter == -1) {
       print "Unable to find release notes for: $product" if($_WEB == 0);
       return;
    }

    &HTMLHeaderFormat(($in_bld ne '' ? 
                               &getRnTitle($product, $chapter[0]) : $product));
    &bigFormat($product);

    my($i) = 0;
    for($i=1; $i < (@chapter + 0); $i++) {
      print "&nbsp;&nbsp;<A href=\"\#$chapter[$i]\"><b>$chapter[$i]</b></A>\n";
      $i++;
    }

    &formatRnChapters(@chapter);
    return;
}


#######################################################################
#
# findRnMatches(String $rnbase, String $rncmd)
#
# Find release note
#
#######################################################################

sub findRnMatches{

    my($rnbase,$rncmd,$rnpath) = @_;
    my(@rnfiles) = ();
    open(RNPIPE, $rncmd) || die "$rncmd: !$\n";

    while(<RNPIPE>){
        # Get the filename; compare to the size of the base
        s/\n//;
        if($_ ne ""){
           $tmp= sprintf("%s/%s", $rnpath, $_);
           if (length($tmp) == length($rnbase)) {
               push(@rnfiles, $tmp);
           }
        }
    }

    return(@rnfiles);
}


#######################################################################
#
# @toc buildRnChapterList(String "filename of TC")
#
#######################################################################

sub buildRnChapterList{

    my($productbase, $tcFile) = @_;
    my @chapterlist     = ();
    my @toc             = ();
    my @chapterfilelist = ();
    my $chapternum      = 0;
    my $title           = "";
    my $chapterfile     = "";
    my $s               = "";

    if( open(TOC,$tcFile) ) {

        # collect all files via table of contents
        #
        while(<TOC>) {
              chop;
              if (/^\d|[A-Z]/) {
                  push(@chapterlist, $_);
              }
        }
        close(TOC);
    }

    # check for other files...
    #
    foreach (`cd ${productbase}; /sbin/ls -1 *`) {
        chop;
        if( ${_} =~ /\.gz$/i || ${_} =~ /\.z$/i
            || 
            ${_} eq 'TC' || ${_} =~ /TRANS/) {
            next;
        }
        $s = "${_}\t${_}";
        push(@chapterlist, $s);
    }
 

    foreach $chapter (@chapterlist) {

        ($chapternum, $title) = split (/\s+/,  $chapter,  2);
        $chapterfile = $productbase . '/ch' . $chapternum . '.z';
        if ( -e $chapterfile && -s $chapterfile > 0 ) {
            push(@chapterfilelist, $chapterfile);
            push(@chapterfilelist, $title);
        } else {

            # dmedia, or relnotes with prepended "0"
            #
            $chapterfile =~ s/ch([\d]+)\.z/ch0$1\.z/;
            if ( -e $chapterfile && -s $chapterfile > 0 ) {
                push(@chapterfilelist, $chapterfile);
                push(@chapterfilelist, $title);
            } else {

                # other files...
                $chapterfile = "${productbase}/${chapternum}";
                if ( -e $chapterfile && -s $chapterfile > 0 ) {
                     push(@chapterfilelist, $chapterfile);
                     push(@chapterfilelist, $title);
                }
            }
        }

    }

    return(@chapterfilelist);
}


#######################################################################
#
# void formatRnChapters(@chapterfilelist)
#
#######################################################################

sub formatRnChapters{

    my(@chaps) = @_;
    my($pgbrkcfmt) = "./pgbrkcfmt";
    my($chapter,$title_id,$filter,$para) = '';
    my($i) = 0;
    my @paraArray = ();

    $/ = undef; # Read in all at once

    for($i=0; $i < (@chaps + 0); $i++ ) {

        $chapter  = $chaps[$i];
        $i++;
        $title_id = $chaps[$i];

        # the sed cmd is to interpert superscripts; must be done before the
        # stream hits col, otherwise formatting is a mess (hence, using sed)
        # couldn't go directly to HTML, cuz the other cmds mangle it
        #
        $filter = "pcat $chapter | col | " .
                  "sed 's/.8r.9/SSrSS/g;s/o.82.9/o2/g' | " .
                  "$pgbrkcfmt ";
        if( $chapter !~ /\.z$/i ) {
            $filter = "cat $chapter ";
        }

        open(RNPIPE, "$filter |") || do {
             print "<b>Unable to open $chapter</b>\n";
             return;
        };

        print "\n",
          ($i > 1 ? "<center><A href=\"\#X\"><font size=\"-1\">back to top</font></A></center>" : ''),
          "<HR ALIGN=\"left\" WIDTH=\"99%\">\n",
          "<A name=\"$title_id\"><B>$title_id</B></A>\n";

        while(<RNPIPE>){

         if($_ eq ''){
            next;
         }

         # plain text docs...
         if( $filter =~ /^cat/ ) { 

             &hilite(\$_);
             print $_;

         } else {

            $_ =~ s/SSrSS/<sup>r<\/sup>/g;
            $_ =~ s/\+\x8//g;

            &pageBreakRemoval(\$_);

            if(/\n\n/){
               @paraArray = split(/\n\n+/, $_);
               foreach $para (@paraArray){
                  &formatRnParagraph(\$para);
                  print "\n";
               }
            }else{
               &formatRnParagraph(\$_);
            }
         }
        }
        close(RNPIPE);
    }

    print "<center>",
          "<A href=\"\#X\"><font size=\"-1\">back to top</font></A>",
          "</center>",
          "<HR ALIGN=\"left\" WIDTH=\"99%\">\n";

    &HTMLTrailerFormat();
    return;
}


#######################################################################
#
# void formatRnParagraph(String_ref \$buf)
#
# Identify a paragraph
#
#######################################################################

sub formatRnParagraph{

    my($buf_ref) = @_;
   
    if($$buf_ref eq '') {
	return;
    }

    # Remove trailing page numbers
    #
    if($$buf_ref =~ /(<B>)?Page(<\/B>)?\ (<B>)?\d\d?(<\/B>)?/
       ||
       $$buf_ref =~ /^[\s\-]+[\d]+[\s\-]+$/) {
        return;
    }

    # At this point we have some sort of body paragraph
    # Apply links and keyword highlighting if necessary
    #
    &addLinks($buf_ref);
    &hilite($buf_ref);

    # this is for a named areas
    #
    if($$buf_ref =~ /^[\s]+([\d\.]+)[\s]+<[BI]+>/) {

       my($tmp) = $$buf_ref;
       my(@num) = ($1 =~ /\./g);
       
       $tmp =~ s/I>/B>/g;
       $tmp =~ s/\_/\ /g;

       if( $tmp =~ /^[\s]+[\d]+[\.]+[\s]+/ ) {
           $tmp =~ s/^[\s]{4}//;
       } elsif( $tmp =~ /^[\s]+[\d]+[\.]+[\d]+[\s]+/ ) {
           $tmp =~ s/^[\s]{5}//;
       }
       print $tmp, "\n", ((@num + 0) == 1 ? "\n" : '');

    } else {

       print $$buf_ref, "\n";
    }
}


#######################################################################
#
# string getRnTitle(product, file)
#
#######################################################################

sub getRnTitle {

    # used in build stage, where speed isn't such an issue
    #
    my($prod, $f) = @_;
    my($ttl) = '';
  
    if( !(-r $f) ) {
        return "$prod";
    }

    if( $f =~ /\.gz$/ ) {
        $ttl = &get_rn_gz_title($f);
    } else {
        $ttl = &get_rn_title($f);
    }

    return "$ttl";
}

sub get_rn_gz_title() {

    my($rn) = @_;
    my(@c) = split(/\//, $rn);
    my($buf, $t) = '';
    my($in_id) = 0;
    my($t) = $c[((@c+0)-2)];

    open(FP, "gunzip -cd $rn |") || do {
         print "mkrnwrdlist: cannot run title cmd ($rn)\n" if($_WEB == 0);
         return "$t";
    };

    while(<FP>) {
         chop;
         if( $_ =~ /<title>(.*?)<\/title>/i ) {
             $buf = $1;
             $buf =~ s/<[^>]*>//gs;
             $buf =~ s/^[\s]+//g;
             $buf =~ s/[\s]+$//g;
             $t .= " : $buf";
             last;
         }
    }
    close(FP);

    return "$t";
}


sub get_rn_title() {

    my($rn) = @_;
    my(@c) = split(/\//, $rn);
    my($buf, $t) = '';
    my($in_id) = 0;
    my($t) = $c[((@c+0)-2)];

    my($cmd) = '';
    if( $rn =~ /\.z$/ ) {
        $cmd = "pcat $rn | col -xb |";
    } else {
        return "$t";
    }

    open(FP, "$cmd") || do {
         print "mkrnwrdlist: cannot run title cmd ($rn)\n" if($_WEB == 0);
         return "$t";
    };

    while(<FP>) {
         chop;

         if( $_ =~ /_Identification_/i ) {
             $in_id = 1;
         }

         if( $in_id > 0 ) {

             $buf = '';

             if( $_ =~ /^[\s]+version[s]*[\s]+(.*?)$/mi ) {

                 $buf = "($1)";
                 $in_id = 3;

             } elsif( $_ =~ /^[\s]+release[\s]+(.*?)$/mi ) {

                 $buf = "($1)";
                 $in_id = 3;

             } elsif( $_ =~ /product[s]?[\s]{8,}(.*?)$/mi ) {

                 $buf = ": $1";
                 $in_id = 2;

             } elsif( $in_id == 2 && $_ =~ /^[\s]{22,}/m ) {

                 $buf = $_;
             }

             if( $buf ne '' ) {
                 $buf =~ s/^[\s]+//g;
                 $buf =~ s/[\s]+$//g;
                 $t .= " $buf";
                 $t =~ s/\-[\ ]+\-//g;
             }

             if( $in_id == 3 ) {
                 last;
             }
         }
    }
    close(FP);

    $t =~ s/[\s]+$//g;
    $t =~ s/\-[\ \d]+\-//;
    return "$t";
}


# Return true from package include
#
1;

